﻿
// <copyright file="SafeListDecorator.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The safe list decorator.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Collections.Concurrent
{
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    using SubhadraSolutions.Sharp.Utils.Validation;

    /// <summary>
    /// The safe list decorator.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public class SafeListDecorator<T> : SafeCollectionDecorator<T>, IList<T>, ISerializable
    {
        #region Fields

        /// <summary>
        /// The adaptedObject.
        /// </summary>
        private readonly IList<T> adaptedObject;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="SafeListDecorator{T}"/> class.
        /// </summary>
        /// <param name="adaptedObject">
        /// The adaptedObject.
        /// </param>
        public SafeListDecorator(IList<T> adaptedObject)
            : base(adaptedObject)
        {
            Guard.ArgumentNotNull(adaptedObject, "adaptedObject");
            this.adaptedObject = adaptedObject;
        }

        #endregion Constructors and Finalizers

        #region Public Indexers

        /// <summary>
        /// The this.
        /// </summary>
        /// <param name="index">
        /// The index.
        /// </param>
        /// <returns>
        /// The <see cref="T"/>.
        /// </returns>
        public T this[int index]
        {
            get
            {
                try
                {
                    this.LockSlim.EnterReadLock();
                    return this.adaptedObject[index];
                }
                finally
                {
                    this.LockSlim.ExitReadLock();
                }
            }

            set
            {
                try
                {
                    this.LockSlim.EnterWriteLock();
                    this.adaptedObject[index] = value;
                }
                finally
                {
                    this.LockSlim.ExitWriteLock();
                }
            }
        }

        #endregion Public Indexers

        #region Public Methods and Operators

        /// <summary>
        /// The get object data.
        /// </summary>
        /// <param name="info">
        /// The info.
        /// </param>
        /// <param name="context">
        /// The context.
        /// </param>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("adaptedObject", this.adaptedObject, this.adaptedObject.GetType());
        }

        /// <summary>
        /// The index of.
        /// </summary>
        /// <param name="item">
        /// The item.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public int IndexOf(T item)
        {
            try
            {
                this.LockSlim.EnterReadLock();
                return this.adaptedObject.IndexOf(item);
            }
            finally
            {
                this.LockSlim.ExitReadLock();
            }
        }

        /// <summary>
        /// The Insert.
        /// </summary>
        /// <param name="index">
        /// The index.
        /// </param>
        /// <param name="item">
        /// The item.
        /// </param>
        public void Insert(int index, T item)
        {
            try
            {
                this.LockSlim.EnterWriteLock();
                this.adaptedObject.Insert(index, item);
            }
            finally
            {
                this.LockSlim.ExitWriteLock();
            }
        }

        /// <summary>
        /// The remove at.
        /// </summary>
        /// <param name="index">
        /// The index.
        /// </param>
        public void RemoveAt(int index)
        {
            try
            {
                this.LockSlim.EnterWriteLock();
                this.adaptedObject.RemoveAt(index);
            }
            finally
            {
                this.LockSlim.ExitWriteLock();
            }
        }

        #endregion Public Methods and Operators
    }
}